import { Request, Response, NextFunction } from "express";
import path from "path";
import jimp from "jimp";
import { err as aaa, err } from "./infoerr";

export default async function uploadhandle(
  req: Request,
  res: Response,
  next: NextFunction
) {
  const oImgPath = path.join(__dirname, "../../public/img/hutao.png");
  const nImgPath = path.join(__dirname, "../../public" + req.query.fileurl);
  const outPutPath = path.join(__dirname, "../../public/upload");
  fun(oImgPath, nImgPath, outPutPath);
  next();
}

async function fun(wf: string, of: string, target: string, p = 5, mar = 0.05) {
  const [n, o] = await Promise.all([jimp.read(wf), jimp.read(of)]);
  const cp = o.bitmap.width / n.bitmap.width;
  wf.search((cp / p) as unknown as string);
  const r = o.bitmap.width * mar;
  const b = o.bitmap.height * mar;
  const x = o.bitmap.width - r - n.bitmap.width;
  const y = o.bitmap.height - b - n.bitmap.height;

  o.composite(n, x, y, {
    mode: jimp.BLEND_ADD,
    opacityDest: 1,
    opacitySource: 0.6,
  });
  o.write(path.resolve(target, path.basename(of)));
}
